
$(function(){
     var eid = getUrlParam("id")
     $.ajax({
          type: "get",
          //TODO:
          url: "/app/commodityInfo/getDetail/"+ eid,
          data: '',
          dataType: "json",
          success: function(mag){
               if (mag.success == true) {
                    var data = mag.data[0]
                    var pic = ''
                    for (let i = 0; i < data.picture.length; i++) {
                         var pic1 = '<li><a href="javascript:void(0)"><img src="https://vm.fowotek.com'+ data.picture[i].picture_url +'" /></a></li>'
                         
                         pic += pic1
                    }
                    $('.goods-details_pic_info').html(pic)
                    TouchSlide({
                         slideCell:"#slideBox",
                         titCell:".hd ul", //开启自动分页 autoPage:true ，此时设置 titCell 为导航元素包裹层
                         mainCell:".bd ul",
                         effect:"leftLoop",
                         autoPage:true,//自动分页
                         // autoPlay:true //自动播放
                    });
                    $('.gdetails-price-text1').html(data.specifications[0].price)
                    // $('.gdetails-price-text2').html()
                    $('.gdetails-price-text3').html(data.specifications[0].specifications)
                    $('.goodsName_info').html(data.commodityName)
                    $('.goodsfenlei_info').html(data.category.categoryName)

                    //加入购物车
                    $(document).on('click','.addcar',function(){
                         
                         //TODO:加入购物车
                         var userName = sessionStorage.getItem("sessionName")
                         var specificationsid = data.specifications[0].id
                         if (userName == null || userName == '' || userName == undefined) {
                              layer.open({
                                   content: "请先去登录账号",
                                   skin: 'msg',
                                   time: 2 //2秒后自动关闭
                              });
                         }else{
                              $.ajax({
                                   type: "get",
                                                            //TODO:
                                   url: "/app/userInfo/" + userName,
                                   data: '',
                                   dataType: "json",
                                   success: function(msg){
                                        $.ajax({
                                             type: "post",
                                                       //TODO:
                                             url: "/app/basket/addCommodityToBasket",
                                             data: {commodity_id: eid,commodity_specification_id: specificationsid,count: 1,user_id: msg.data.id},
                                             dataType: "json",
                                             success: function(res){
                                                  layer.open({
                                                       content: res.message,
                                                       skin: 'msg',
                                                       time: 2 //2秒后自动关闭
                                                  });
                                             }
                                        })
                                   },
                                   error: function(){
                                        layer.open({
                                             content: '登录状态失效，请重新登录',
                                             skin: 'msg',
                                             time: 2 //2秒后自动关闭
                                        });
                                   }
                              })
                         }
                    })

                    //结算订单跳转页面
                    $(document).on('click','.btn2',function(){
                         var userName = sessionStorage.getItem("sessionName")
                         var specificationsid = data.specifications[0].id
                         if (userName == null || userName == '' || userName == undefined) {
                              layer.open({
                                   content: "请先去登录账号",
                                   skin: 'msg',
                                   time: 2 //2秒后自动关闭
                              });
                         }else{
                              $.ajax({
                                   type: "get",
                                                            //TODO:
                                   url: "/app/userInfo/" + userName,
                                   data: '',
                                   dataType: "json",
                                   success: function(msg){
                                        $.ajax({
                                             type: "post",
                                                       //TODO:
                                             url: "/app/basket/addCommodityToBasket",
                                             data: {commodity_id: eid,commodity_specification_id: specificationsid,count: 1,user_id: msg.data.id},
                                             dataType: "json",
                                             success: function(res){
                                                  window.location = "../shoping-cart.html"
                                             }
                                        })
                                   },
                                   error: function(){
                                        layer.open({
                                             content: '登录状态失效，请重新登录',
                                             skin: 'msg',
                                             time: 2 //2秒后自动关闭
                                        });
                                   }
                              })
                         }
                    })
                    
               }else{
                    layer.open({
                         content: '查询失败，请重新登录',
                         skin: 'msg',
                         time: 2 //2秒后自动关闭
                    });
               }
          }
     })
 
})



//获取后台传参
function getUrlParam(name) {
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
     if (r != null) return unescape(r[2]);
     return null;
 }

